package com.hzgj.bcl.soa;

public class Event {
    public static final int MSG = 1;  // arg = Message
    public static final int CONNECT = 2;  // arg = cluster name (string)
    public static final int DISCONNECT = 4;  // arg = member address (Address)
    public static final int BLOCK = 10;  // arg = null (used by FLUSH)
    public static final int FIND_MBRS = 11;  // arg = List<Address> (can be null) -> Responses
    public static final int FIND_INITIAL_MBRS = 12;  // arg = null -> Responses
    public static final int BECOME_SERVER = 16;  // sent when api has joined group
    public static final int GET_APPLSTATE = 17;  // get state from appl (arg=StateTransferInfo)
    public static final int GET_STATE = 19;  // arg = StateTransferInfo
    public static final int GET_STATE_OK = 20;  // arg = StateTransferInfo
    public static final int STABLE = 30;  // arg = long[] (stable seqnos for mbrs)
    public static final int GET_DIGEST = 39;  // arg= address (or null)
    public static final int SET_DIGEST = 41;  // arg = Digest
    public static final int OVERWRITE_DIGEST = 42;  // arg = Digest
    public static final int CONFIG = 56;  // arg = Map<String,Object> (config properties)
    public static final int SUSPEND_STABLE = 65;  // arg = Long (max_suspend_time)
    public static final int RESUME_STABLE = 66;  // arg = null
    public static final int SUSPEND = 68;  // arg = List<Address> (used by FLUSH)
    public static final int RESUME = 70;  // arg = null (used by FLUSH)
    public static final int UNBLOCK = 75;  // arg = null (indicate end of flush round)
    public static final int CLOSE_BARRIER = 76;  // arg = null
    public static final int OPEN_BARRIER = 77;  // arg = null
    public static final int REBROADCAST = 78;  // arg = Digest
    public static final int CONNECT_WITH_STATE_TRANSFER = 80;  // arg = cluster name (string)
    public static final int GET_PHYSICAL_ADDRESS = 87;  // arg = Address --> PhysicalAddress
    public static final int GET_LOGICAL_PHYSICAL_MAPPINGS = 88;  // arg = boolean --> Map<Address,PhysicalAddress>
    public static final int SET_PHYSICAL_ADDRESS = 89;  // arg = Tuple<Address,PhysicalAddress> --> boolean
    public static final int REMOVE_ADDRESS = 90;  // arg = Address
    public static final int GET_LOCAL_ADDRESS = 91;  // arg = null --> UUID (local_addr)
    public static final int CONNECT_USE_FLUSH = 92;
    public static final int CONNECT_WITH_STATE_TRANSFER_USE_FLUSH = 93;
    public static final int SUSPEND_BUT_FAIL = 94; // used in FLUSH testing, no args
    public static final int LOCK = 95; // arg = LockInfo
    public static final int UNLOCK = 96; // arg = LockInfo
    public static final int UNLOCK_ALL = 97; // arg = null
    public static final int LOCK_AWAIT = 98; // arg = LockInfo
    public static final int LOCK_SIGNAL = 99; // arg = AwaitInfo
    public static final int FAILOVER = 100; // returns true or false
    public static final int GET_PHYSICAL_ADDRESSES = 102; // arg = null (returns all physical addresses)
    public static final int FORWARD_TO_COORD = 105; // arg = HttpMessage
    public static final int PUNCH_HOLE = 106; // arg = Address (member)
    public static final int CLOSE_HOLE = 107; // arg = Address (member)
    public static final int REINVOKE = 108;
    public static final int GET_PING_DATA = 109; // arg = cluster_name

    public static final int USER_DEFINED = 1000; // arg = <user def., e.g. evt type + data>


    public static final Event GET_DIGEST_EVT = new Event(Event.GET_DIGEST);

    private final int type;       // type of event
    private final Object arg;        // must be serializable if used for inter-chainStack communication


    public Event(int type) {
        this.type = type;
        this.arg = null;
    }

    public Event(int type, Object arg) {
        this.type = type;
        this.arg = arg;
    }

    public static String type2String(int t) {
        switch (t) {
            case MSG:
                return "MSG";
            case CONNECT:
                return "CONNECT";
            case DISCONNECT:
                return "DISCONNECT";
            case BLOCK:
                return "BLOCK";
            case FIND_MBRS:
                return "FIND_MBRS";
            case FIND_INITIAL_MBRS:
                return "FIND_INITIAL_MBRS";
            case BECOME_SERVER:
                return "BECOME_SERVER";
            case GET_APPLSTATE:
                return "GET_APPLSTATE";
            case GET_STATE:
                return "GET_STATE";
            case GET_STATE_OK:
                return "GET_STATE_OK";
            case STABLE:
                return "STABLE";
            case GET_DIGEST:
                return "GET_DIGEST";
            case SET_DIGEST:
                return "SET_DIGEST";
            case OVERWRITE_DIGEST:
                return "OVERWRITE_DIGEST";
            case CONFIG:
                return "CONFIG";
            case SUSPEND_STABLE:
                return "SUSPEND_STABLE";
            case RESUME_STABLE:
                return "RESUME_STABLE";
            case SUSPEND:
                return "SUSPEND";
            case SUSPEND_BUT_FAIL:
                return "SUSPEND_BUT_FAIL";
            case RESUME:
                return "RESUME";
            case UNBLOCK:
                return "UNBLOCK";
            case CLOSE_BARRIER:
                return "CLOSE_BARRIER";
            case OPEN_BARRIER:
                return "OPEN_BARRIER";
            case REBROADCAST:
                return "REBROADCAST";
            case CONNECT_WITH_STATE_TRANSFER:
                return "CONNECT_WITH_STATE_TRANSFER";
            case GET_PHYSICAL_ADDRESS:
                return "GET_PHYSICAL_ADDRESS";
            case GET_LOGICAL_PHYSICAL_MAPPINGS:
                return "GET_LOGICAL_PHYSICAL_MAPPINGS";
            case SET_PHYSICAL_ADDRESS:
                return "SET_PHYSICAL_ADDRESS";
            case REMOVE_ADDRESS:
                return "REMOVE_ADDRESS";
            case GET_LOCAL_ADDRESS:
                return "GET_LOCAL_ADDRESS";
            case CONNECT_USE_FLUSH:
                return "CONNECT_USE_FLUSH";
            case CONNECT_WITH_STATE_TRANSFER_USE_FLUSH:
                return "CONNECT_WITH_STATE_TRANSFER_USE_FLUSH";
            case LOCK:
                return "LOCK";
            case UNLOCK:
                return "UNLOCK";
            case UNLOCK_ALL:
                return "UNLOCK_ALL";
            case LOCK_AWAIT:
                return "LOCK_AWAIT";
            case LOCK_SIGNAL:
                return "LOCK_SIGNAL";
            case FAILOVER:
                return "FAILOVER";
            case GET_PHYSICAL_ADDRESSES:
                return "GET_PHYSICAL_ADDRESSES";
            case FORWARD_TO_COORD:
                return "FORWARD_TO_COORD";
            case PUNCH_HOLE:
                return "PUNCH_HOLE";
            case CLOSE_HOLE:
                return "CLOSE_HOLE";
            case REINVOKE:
                return "REINVOKE";
            case GET_PING_DATA:
                return "GET_PING_DATA";

            case USER_DEFINED:
                return "USER_DEFINED";
            default:
                return "UNDEFINED(" + t + ")";

        }
    }

    public final int getType() {
        return type;
    }

    public Object getArg() {
        return arg;
    }
}
